package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.mapper.IntegralGradeMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2023-01-04
 */
@Service
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {
    @Resource
    UserInfoService userInfoService;
    @Override
    public BigDecimal getborrowAmount(String token) {
        Long userId = JwtUtils.getUserId(token);
        //查询用户的积分
        UserInfo userInfo = userInfoService.getById(userId);
        Integer integral = userInfo.getIntegral();
        // select * from integral_grade where  start <= integral and end>=integral
        IntegralGrade integralGrade = this.getOne(Wrappers.lambdaQuery(IntegralGrade.class)
                .le(IntegralGrade::getIntegralStart, integral)
                .ge(IntegralGrade::getIntegralEnd, integral));
        if(integralGrade==null){
            return new BigDecimal(0);
        }
        return integralGrade.getBorrowAmount();
    }
}
